package com.haoyeteng.sxbd.modular.business.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;
import com.haoyeteng.sxbd.core.constant.SxbdConstants;
import com.haoyeteng.sxbd.core.exception.SxbdException;
import com.haoyeteng.sxbd.core.util.DateUtil;
import com.haoyeteng.sxbd.core.util.GeometryUtil;
import com.haoyeteng.sxbd.core.util.HttpUtils;
import com.haoyeteng.sxbd.core.util.ToolUtil;
import com.haoyeteng.sxbd.modular.business.model.*;
import com.haoyeteng.sxbd.modular.business.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.common.annotion.Permission;

import java.awt.geom.Point2D;
import java.util.*;

import com.haoyeteng.sxbd.core.log.LogObjectHolder;

/**
 * 报警表控制器
 *
 * @author haoyeteng
 * @date 2019-07-06 23:49:23
 */
@Controller
@RequestMapping("/warn")
public class WarnController extends BaseController {

    private final String PREFIX = "/business/warn/";

    @Autowired
    private IWarnService warnService;

    @Autowired
    private IEleFenceService eleFenceService;

    /**
     * 跳转到报警表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "warn.html";
    }

    /**
     * 获取报警表列表
     */
//    @RequestMapping(value = "/list")
//    @ResponseBody
//    @Permission
//    public ResponseResult<Object> list(String serviceType, String account, String startDate, String endDate) {
//        Page<Warn> page = new PageFactory<Warn>().defaultPage();
//        Map<String, Object> params = new HashMap<>();
//        params.put("serviceType", serviceType);
//        params.put("account", account);
//        params.put("startDate", startDate);
//        params.put("endDate", endDate);
//        List<Map<String, Object>> warns = warnService.selectWarns(page, params);
//        ResponseResult<Object> result = new ResponseResult<Object>();
//        result.setData(warns);
//        result.setCount(page.getTotal());
//        result.setCode(SUCCESS_TIP.getCode());
//        result.setMsg(SUCCESS_TIP.getMessage());
//        return result;
//    }

    /**
     * 获取报警表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public ResponseResult<Object> locatAllpositon(String serviceType, String account) {
        Page<Map<String,Object>> page = new PageFactory< Map<String,Object>>().defaultPage();
        ResponseResult<Object> result = new ResponseResult<Object>();
        List<Map<String, Object>> list = warnService.getWarns(serviceType, account);
        int fromIndex = (page.getCurrent() - 1) * page.getLimit();
        int toIndex = page.getCurrent() * page.getLimit();
        result.setData(list.subList(fromIndex > list.size() ? 0 : fromIndex , toIndex > list.size() ? list.size() : toIndex));
        result.setCount(list.size());
        result.setCode(SUCCESS_TIP.getCode());
        result.setMsg(SUCCESS_TIP.getMessage());
        return result;
    }

    /**
     * 查看报警
     */
    @RequestMapping(value = "/detail",method = RequestMethod.POST)
    public Object detail(Coordinate coordinate, Model model) {
        EleFence eleFence = eleFenceService.selectAndDtlById(coordinate.getEleFenceId());
        model.addAttribute("details", net.sf.json.JSONArray.fromObject(eleFence.getDetails()));
        model.addAttribute("coordinate", coordinate);
        return PREFIX + "warn_detail.html";
    }
}
